package com.api.oa.services.impl;

import cn.hutool.core.util.StrUtil;
import com.api.oa.mapper.OaOrgNoticeMapper;
import com.api.oa.mapper.OaOrgNoticeRelationMapper;
import com.api.oa.mapper.OrgMapper;
import com.api.oa.model.entity.OaNotice;
import com.api.oa.model.entity.OaOrg;
import com.api.oa.model.entity.OaRelation;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.OaRelationVO;
import com.api.oa.services.IOaOrgNoticeService;
import com.api.shopapi.entity.dto.MedicinerDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通知
 */
@Service
public class OaOrgNoticeServiceImpl implements IOaOrgNoticeService {

    @Autowired
    private OaOrgNoticeMapper oaOrgNoticeMapper;

    @Autowired
    private OaOrgNoticeRelationMapper oaOrgNoticeRelationMapper;

    @Autowired
    private OrgMapper orgMapper;

    @Override
    @Transactional
    public OaNotice add(OaNotice oaOrg) throws Exception {
        if(oaOrg.getNoticeId() == null){
            OaOrg oneByUserId = orgMapper.findOneByUserId(oaOrg.getUserId());
            if(oneByUserId == null){
                throw new Exception("不是项目机构的负责人，不可以发布通知");
            }
            oaOrg.setNoticeOrgId(oneByUserId.getOrgId());
            oaOrg.setNoticeTime(new Date());
            oaOrg.setModifyTime(new Date());
            oaOrg.setIsTop(0);

            oaOrgNoticeMapper.insert(oaOrg);
            if(!StrUtil.isEmptyIfStr(oaOrg.getToOrgIds())) {
                oaOrg.getToOrgIds().stream().map(i->{
                    OaRelation oaRelation = new OaRelation();
                    oaRelation.setRelationNoticeId(oaOrg.getNoticeId());
                    oaRelation.setRelationOrgId(i);
                    oaRelation.setIsRead(0);
                    oaRelation.setUploadFileId(0);
                    oaRelation.setUpdatedAt(new Date());
                    return oaRelation;
                }).forEach(i->{
                    oaOrgNoticeRelationMapper.insert(i);
                });
            }
        }else{
            this.update(oaOrg);
        }

        return oaOrg;
    }

    @Override
    public void remove(Long id) {
        oaOrgNoticeMapper.delete(id);
    }

    @Override
    public Integer update(OaNotice org) {
        return oaOrgNoticeMapper.update(org);
    }

    @Override
    public PageInfo<OaNotice> listFrom(String userId,Long isDraft,Integer pageNo,Integer pageSize) {
        PageInfo<OaNotice> pageInfo;
        OaOrg oneByUserId = orgMapper.findOneByUserId(userId);
        if(oneByUserId == null){
            return new PageInfo<>(new ArrayList<>());
        }
         PageHelper.startPage(pageNo, pageSize);
         pageInfo = new PageInfo<>(oaOrgNoticeMapper.listFrom(oneByUserId.getOrgId(),isDraft));
        return pageInfo;
    }

    @Override
    public PageInfo<OaRelationVO> listUnderNotice(Long noticeId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<OaRelationVO> pageInfo = new PageInfo<>(oaOrgNoticeRelationMapper.listUnderNotice(noticeId));
        return pageInfo;
    }



    @Override
    public OaNotice one(Long id) {
        return oaOrgNoticeMapper.findOne(id);
    }

    @Override
    public PageInfo<OaNoticeVo> listTo(String uid, Integer isRead, Integer pageIndex, Integer pageSize) {
        PageInfo<OaNoticeVo> pageInfo;
        OaOrg oneByUserId = orgMapper.findOneByUserId(uid);
        if(oneByUserId == null){
            return new PageInfo<>(new ArrayList<>());
        }
        PageHelper.startPage(pageIndex,pageSize);
        pageInfo = new PageInfo<>(oaOrgNoticeMapper.listTo(oneByUserId.getOrgId(),isRead));
        return pageInfo;
    }


    @Override
    public void readNotice(Long id) {
        oaOrgNoticeRelationMapper.updateRead(id,1);
    }

    @Override
    public Integer noticeReplay(Long id, Long fileId) {
        return oaOrgNoticeRelationMapper.noticeReplay(id,fileId);
    }
}
